package com.duanjw.bank.rate.handler;

import cn.hutool.json.JSONObject;
import com.duanjw.bank.rate.core.BankRate;
import com.duanjw.bank.rate.core.BankRateHandler;
import lombok.SneakyThrows;
import org.jsoup.Connection;
import org.jsoup.Jsoup;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.Map;

/**
 * 四川银行
 *
 * @author duanjw
 * @date 2024/4/1
 */
public class SiChuanBanRateHandler implements BankRateHandler {
    @Override
    public BankRate getBankRate() throws IOException {
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        headers.put("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36"); // 仿真
        Connection connect = Jsoup.connect("https://www.scbank.cn/ajaxLv");
        String requestBody = "{\"pro_type\":\"1\"}";
        String responseBody = connect.requestBody(requestBody).headers(headers).ignoreContentType(true).method(Connection.Method.POST).execute().body();

        JSONObject responseJson = new JSONObject(responseBody).getJSONObject("info");

        String kaiShiZhiXingRiQi = getKaiShiZhiXingRiQi(null);

        String huoQiCunKuanLiLv = responseJson.getStr("R001");

        String zhengCunZhengQu3YueLiLv = responseJson.getStr("R002");
        String zhengCunZhengQu6YueLiLv = responseJson.getStr("R003");
        String zhengCunZhengQu12YueLiLv = responseJson.getStr("R004");
        String zhengCunZhengQu24YueLiLv = responseJson.getStr("R005");
        String zhengCunZhengQu36YueLiLv = responseJson.getStr("R006");
        String zhengCunZhengQu60YueLiLv = responseJson.getStr("R007");

        String lingCunZhengQu12YueLiLv = responseJson.getStr("R008");
        String lingCunZhengQu36YueLiLv = responseJson.getStr("R009");
        String lingCunZhengQu60YueLiLv = responseJson.getStr("R010");


        String dingHuoLiangBian3YueLiLv = new BigDecimal(zhengCunZhengQu3YueLiLv).multiply(new BigDecimal("0.6")).setScale(3, RoundingMode.HALF_UP).toString();
        String dingHuoLiangBian6YueLiLv = new BigDecimal(zhengCunZhengQu6YueLiLv).multiply(new BigDecimal("0.6")).setScale(3, RoundingMode.HALF_UP).toString();
        String dingHuoLiangBian12YueLiLv = new BigDecimal(zhengCunZhengQu12YueLiLv).multiply(new BigDecimal("0.6")).setScale(3, RoundingMode.HALF_UP).toString();

        String tongZhiCunKuan1TianLiLv = responseJson.getStr("R012");
        String tongZhiCunKuan7TianLiLv = responseJson.getStr("R013");

        String xieDingCunKuanLiLv = null;

        return new BankRate(null, huoQiCunKuanLiLv, zhengCunZhengQu3YueLiLv, zhengCunZhengQu6YueLiLv, zhengCunZhengQu12YueLiLv, zhengCunZhengQu24YueLiLv, zhengCunZhengQu36YueLiLv, zhengCunZhengQu60YueLiLv, lingCunZhengQu12YueLiLv, lingCunZhengQu36YueLiLv, lingCunZhengQu60YueLiLv, dingHuoLiangBian3YueLiLv, dingHuoLiangBian6YueLiLv, dingHuoLiangBian12YueLiLv, tongZhiCunKuan1TianLiLv, tongZhiCunKuan7TianLiLv, xieDingCunKuanLiLv);
    }

    @Override
    public String getBankName() {
        return "四川银行";
    }

    @SneakyThrows
    private String getKaiShiZhiXingRiQi(String input) {
        return null;
    }
}
